package io.github.talelin.latticy.service.impl.ag;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.github.talelin.latticy.dto.ag.building.CreateOrUpdateBuildingDTO;
import io.github.talelin.latticy.mapper.ag.BuildingMapper;
import io.github.talelin.latticy.model.ag.BuildingDO;
import io.github.talelin.latticy.service.ag.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {

    @Autowired
    BuildingMapper buildingMapper;

    @Override
    public BuildingDO getById(int id) {
        BuildingDO building = buildingMapper.selectById(id);
        return building;
    }

    @Override
    public List<BuildingDO> getBuildings() {
        QueryWrapper<BuildingDO> wrapper = new QueryWrapper();
        wrapper.select().orderByDesc("update_time");
        List<BuildingDO> buildingList = buildingMapper.selectList(wrapper);
        return buildingList;
    }

//    //只返回楼栋Id和楼栋名字
//    @Override
//    public List<BuildingDO> getBuildingName() {
//        QueryWrapper<BuildingDO> wrapper = new QueryWrapper();
//        wrapper.select("id","name");
//        List<BuildingDO> buildingNameList = buildingMapper.selectList(wrapper);
//        return buildingNameList;
//    }

    @Override
    public boolean createBuilding(CreateOrUpdateBuildingDTO validator) {
        System.out.println(validator);
        BuildingDO building = new BuildingDO();
        building.setName(validator.getName());
        building.setLocal(validator.getLocal());
        building.setCoordinates(validator.getCoordinates());
        building.setManager(validator.getManager());
        building.setPhone(validator.getPhone());
        return buildingMapper.insert(building) > 0;
    }

    @Override
    public boolean deleteById(int id) {
        return buildingMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateBuilding(BuildingDO building,CreateOrUpdateBuildingDTO validator) {
        building.setName(validator.getName());
        building.setLocal(validator.getLocal());
        building.setCoordinates(validator.getCoordinates());
        building.setManager(validator.getManager());
        building.setPhone(validator.getPhone());
        return buildingMapper.updateById(building) > 0;
    }
}
